package com.example.mybatisplus.service.impl;

import com.example.mybatisplus.mapper.ConsultMergeMapper;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.mapper.VisitLogMapper;
import com.example.mybatisplus.model.domain.*;
import com.example.mybatisplus.mapper.VisitResultMapper;
import com.example.mybatisplus.service.VisitResultService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2022-05-31
 */
@Service
public class VisitResultServiceImpl extends ServiceImpl<VisitResultMapper, VisitResult> implements VisitResultService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    VisitResultMapper visitResultMapper;
    @Autowired
    VisitLogMapper visitLogMapper;
    @Autowired
    ConsultMergeMapper consultMergeMapper;

    @Override
    public String addvisitRessult(VisitResult visitResult,String teausername) {

        User teacher=userMapper.getUserbyName(teausername);

        if(visitLogMapper.getvisitlogbyStuId(visitResult.getStuId())==null){
            return "初访记录表中无记录，无法添加";
        }
        else if(visitLogMapper.getvisitlogbyStuId(visitResult.getStuId()).getTeaId()!=teacher.getId()){
            return "此学生不是当前初访师负责的学生，无法添加";
        }
        else {
            VisitLog visitLog = visitLogMapper.getvisitlogbyStuId(visitResult.getStuId());

            visitResult.setVisId(visitLog.getId());
            visitResult.setTeaId(teacher.getId());

            if(visitResultMapper.getvisitRsultbyteaIdandstuIdandvisitTime(visitResult.getStuId(),visitResult.getTeaId(),visitResult.getVisit_time())!=null){
                return "已有相同结果，无法添加，请点击修改";
            }
            else {

                visitResultMapper.addvisitResult(visitResult);

                visitLogMapper.deletevisitLog(visitResult.getVisId());

//                if(consultMergeMapper.queryConsult(visitResult.getStuId())==null && visitResult.getVisitResult()==4){
//                    ConsultMerge consultMerge=new ConsultMerge();
//                    consultMerge.setStuId(visitResult.getStuId());
//
//                    consultMergeMapper.addConsult(consultMerge);
//                }

                return "添加成功";
            }
        }
    }

    @Override
    public List<VisitResult> getresultbystuId(String stuusername) {

        User student=userMapper.getUserbyName(stuusername);

        return visitResultMapper.getvisitRusultbystuId(student.getId());
    }

    @Override
    public List<VisitResult> getall() {
        return visitResultMapper.getall();
    }

    @Override
    public List<DictData> getdiseaseResuldict() {
        return visitResultMapper.getdiseaseResuldict();
    }
}
